﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Battleborn.Core.Abstract;
using Battleborn.Core.Models;

namespace Battleborn.Website.Controllers
{
    public class BattleController : Controller
    {
        private readonly IBattlebornRepository _battleRepository;

        public BattleController(IBattlebornRepository battleRepository)
        {
            _battleRepository = battleRepository;
        }
        //
        // GET: /Battle/

        public ActionResult Index()
        {
            return View(_battleRepository.Battles);
        }

        // GET: /Battle/Create
        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Battle/Create
        [HttpPost]
        public ActionResult Create(Battle battle)
        {
            if (ModelState.IsValid)
            {
                _battleRepository.SaveBattle(battle);
                return RedirectToAction("Index");
            }

            return View(battle);
        }

        [HttpGet]
        public ActionResult Delete(int id = 0)
        {
            Battle battle = _battleRepository.Battles.Single(c => c.BattleId == id);
            if (battle == null)
            {
                return HttpNotFound();
            }
            return View(battle);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Battle battle = _battleRepository.Battles.Single(c => c.BattleId == id);

            if (battle != null)
            {
                _battleRepository.DeleteBattle(battle);
            }

            return RedirectToAction("Index");
        }

    }
}
